//
//  NewsDetailHotReplyFooterCell.swift
//  甜言蜜语
//
//  Created by yingli_yanfa_liuxingyu on 16/8/25.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit
import SVProgressHUD
protocol NewsDetailHotReplyFooterCellDelegate {
    func userTouched(userID: Int)
    func reloaData()
    
}
class NewsDetailHotReplyFooterCell: UITableViewCell {
    @IBOutlet weak var iv: UIImageView!
    @IBOutlet weak var idLbl: UILabel!
    @IBOutlet weak var timeLbl: UILabel!
    @IBOutlet weak var supposeNumLbl: UILabel!
    @IBOutlet weak var replyNumLbl: UILabel!
    @IBOutlet weak var messageLbl: UILabel!
    @IBOutlet weak var supposeBtn: UIButton!
    @IBOutlet weak var delBtn: UIButton!
    
    var cellHeight: CGFloat?
    var type: String = ""// 分为活动和行业
    var delegate: NewsDetailHotReplyFooterCellDelegate?
    var modelList:NewsDetailHotReplyModel?{
        didSet{
            self.setUpView()
        }
    }
    
    var headType:String = ""// 分为详情和回复

    override func awakeFromNib() {
        super.awakeFromNib()

        self.supposeBtn.addTarget(self, action: #selector(NewsDetailHotReplyFooterCell.tappedButton), forControlEvents: UIControlEvents.TouchUpInside)
        
        self.delBtn.addTarget(self, action: #selector(self.delCommen), forControlEvents: UIControlEvents.TouchUpInside)
        
        self.iv.userInteractionEnabled = true
        
        let singleTap = UITapGestureRecognizer(target: self, action: #selector(NewsDetailHotReplyFooterCell.userTouched))
        self.iv.addGestureRecognizer(singleTap)
    }
    func tappedButton(sender: UIButton){
        if ApplicationDelegate.isLogin(){
            if !sender.selected && self.modelList != nil {
               
                DataTool.supposeComment(Int(self.modelList!.id)!,type: self.type, completionHandler: { (isSuccess) -> Void in
                    if isSuccess {
                        let value = Int(self.modelList!.suppose)! + 1
                        
                        self.supposeNumLbl.text = String(value)
                        self.supposeBtn.setImage(UIImage(named: "zan_selected"), forState: UIControlState.Normal)
                    } else {
                        print("点赞失败" )
                    }
                })
            }

        }else{
            SVProgressHUD.showInfoWithStatus("请先登录")
        }
    }
    func userTouched() {
        print("user touched")
        if self.headType == "详情" {
            let dic = ["id": self.modelList!.user]
            NSNotificationCenter.defaultCenter().postNotificationName(userHeadClicked_noti, object: dic)
        } else if self.headType == "回复" {
            let dic = ["id": self.modelList!.user]
            NSNotificationCenter.defaultCenter().postNotificationName("userHeadClicked_noti_head", object: dic)
        }
        
        
    }
    func delCommen() {
        print("删除自己的评论")
        DataTool.deltecomment((self.modelList?.id)!, completionHandler:{ (newModel) in
            guard newModel != nil else {
                return
            }
            //self.delegate?.reloaData()
            NSNotificationCenter.defaultCenter().postNotificationName("relodData", object: nil)
           
        })
        
    }
    func setCellContentWithCellInfo(text: String) -> Void {
        let font = UIFont(name: "Helvetica", size: 16)
        cellHeight = publicMethod.getLabHeigh(text, font:font!, width:SCREEN_WIDTH - 16) + 67
    }
    
    func setUpView(){
        
        if self.modelList!.isme == true {
            self.delBtn.hidden = false
        } else {
            self.delBtn.hidden = true
        }
        
        self.iv.sd_setImageWithURL(NSURL(string: (self.modelList?.timg)!), placeholderImage: UIImage(named: "placeholder"))
        if self.modelList?.isdianzan == true{
            self.supposeBtn.setImage(UIImage(named: "zan_selected"), forState: UIControlState.Normal)
        }else{
            self.supposeBtn.setImage(UIImage(named: "zan"), forState: UIControlState.Normal)
        }
        self.idLbl.text = self.modelList?.name
        
        self.timeLbl.text = publicMethod.computationTimeDifference((self.modelList?.commenttime)!)
        self.supposeNumLbl.text = self.modelList?.suppose
        self.replyNumLbl.text = self.modelList?.childnum
        self.messageLbl.text = self.modelList?.message
        
        if self.modelList?.isdianzan == true{
            self.supposeBtn.userInteractionEnabled = false
        }else{
            self.supposeBtn.userInteractionEnabled = true
        }
        
    }
}

